const mysql = require('mysql');

const connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '',
    database: 'mapcache'
});

connection.connect();
function get(reqArry, response, request) {
    let type = reqArry[1];
    let zoom = reqArry[2];
    let x = reqArry[3];
    let y = reqArry[4];
    // if ((zoom == 3 && x == 1) || (zoom == 5 && y == 17) || (zoom == 4 && y == 3) || (zoom == 3 && y == 1) || (zoom == 4 && x == 3)|| (zoom == 4 && x == 1) || (zoom == 4 && y == 4) || (zoom == 3 && y == 5) || (zoom == 3 && x == 6) || (zoom == 3 && y == 6)){
    //   console.log(11111111111)
    //   connection.query(`select Tile from gmapnetcache where Type=${506225996} and Zoom=${5} and X=${8} and Y=${13}`, function (error, results, fields) {
    //     response.statusCode = 200;
    //     response.setHeader('Content-Type', 'image/png');
    //     response.write(results[0].Tile, "binary");
    //     response.end();
    //   });
    // } else {
      connection.query(`select Tile from gmapnetcache where Type=${type} and Zoom=${zoom} and X=${x} and Y=${y}`, function (error, results, fields) {
        if (error || !results || !results[0]) {
          response.statusCode = 500;
          response.setHeader("Content-Type", "text/plain");
          response.write(error + "\n");
          response.end();
          return;
        };
        response.statusCode = 200;
        response.setHeader('Content-Type', 'image/png');
        response.write(results[0].Tile, "binary");
        response.end();
      });
    // }
}

exports.get = get;